package net.kts.story.menu;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import net.kts.story.progress.StoryProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

class ImageDownloadTask implements Runnable{
	private URL imageURL;
	private ImageView imageView;
	
	public ImageDownloadTask(URL imageURL, ImageView imageView) {
		super();
		this.imageURL = imageURL;
		this.imageView = imageView;
	}
	@Override
	public void run() {
		createImageView(imageURL, imageView);
	}	
	private void createImageView(URL imageURL, final ImageView imageView) {
		BufferedInputStream  bis = null;
		try {
			URLConnection conn =  imageURL.openConnection(); 
			conn.connect(); 
			bis = new BufferedInputStream(conn.getInputStream());
			final Bitmap bm = BitmapFactory.decodeStream(bis); bis.close();
			
			StoryProgressDialog.doItExecute(new Runnable() {
				@Override
				public void run() {
					imageView.setImageBitmap(bm);
				}
			});
			
			bis.close();
		} catch (IOException e) {
			Log.e("kts",e.getMessage());
		}finally{
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					Log.e("kts",e.getMessage());
				}
			}		
			StoryProgressDialog.dismissProgressDiaglog();
		}
	}	
}